package com.boarsoft.boar.sys.action;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.boarsoft.bean.ReplyInfo;
import com.boarsoft.boar.common.Constants;
import com.boarsoft.boar.sys.SysMessager;
import com.boarsoft.boar.sys.SysVerifyBiz;
import com.boarsoft.boar.sys.entity.SysVerify;
import com.boarsoft.common.Authorized;

@Component
@RestController
@RequestMapping("/sys/verify")
public class SysVerifyAction {
	@Autowired
	private SysVerifyBiz sysVerifyBiz;
	@Autowired
	private SysMessager sysMessager;

	/**
	 * 生成并发送验证码
	 * @return 
	 */
	@RequestMapping("/make.do")
	@Authorized(code = "sys.verify.make")
	public ReplyInfo<Object> make(String code) {
		SysVerify v = sysMessager.makeAndSend(code);
		if (v == null) {
			return Constants.REPLY_WARN_INVALID;
		}
		sysVerifyBiz.save(v);
		return ReplyInfo.SUCCESS;
	}

	/**
	 * 检查邮箱或手机+验证码
	 * @return 
	 */
	@RequestMapping("/check.do")
	@Authorized(code = "sys.verify.check")
	public ReplyInfo<Object> check(String u, String v) {
		SysVerify o = sysVerifyBiz.check(u, v);
		if (o == null) {
			return Constants.REPLY_INFO_NOTFOUND;
		}
		sysVerifyBiz.delete(o);
		return ReplyInfo.SUCCESS;
	}

}